<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>登录</title>
	</head>
	<body>
		<a
			href="https://login.dingtalk.com/oauth2/challenge.htm?redirect_uri=http://3404c01h14.qicp.vip/web/JavaScript-edu/dingtalk/index.html&response_type=code&client_id=dingjgw9j0bnfgwhvz0u&scope=openid&state=dddd&prompt=consent">钉钉登录</a>
		<div class="container">
			<div class="avatar">
				<img src="" alt="" id="avatar">
			</div>
			<div class="info"><span id="name"></span></div>
		</div>
	</body>
</html>
<script type="text/javascript">
	function getUserInfo(code) {
		var ajax = new XMLHttpRequest();
		ajax.open('get', 'http://3404c01h14.qicp.vip/service/auth?authCode=' + code);
		ajax.send();
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4 && ajax.status == 200) {
				console.log(ajax.responseText); //输入相应的内容
				let user = JSON.parse(ajax.responseText);
				document.getElementById("avatar").src=user.avatarUrl;
				document.getElementById("name").innerText=user.nick;
			}
		}
	}
	function getQueryVariable(variable)
	{
	       var query = window.location.search.substring(1);
	       var vars = query.split("&");
	       for (var i=0;i<vars.length;i++) {
	               var pair = vars[i].split("=");
	               if(pair[0] == variable){return pair[1];}
	       }
	       return(false);
	}
	
	window.onload=function(){
		let authCode = getQueryVariable("authCode");
		console.log("authCode:",authCode)
		if(authCode){
			getUserInfo(authCode);
		}
	}
</script>
